<!--
 * @Description: 系统配置界面
-->
<template >
  <div>
    <!-- 新增按钮开始 -->
    <div>
      <el-button type="primary" @click='toAdd'>新增</el-button>
    </div>
    <!-- 新增按钮结束 -->
    <!-- 表格开始 -->
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column type="index" :index="1" label="序号" width="200"></el-table-column>
        <el-table-column prop="name" label="名称" width="200"></el-table-column>
        <el-table-column prop="val" label="值" width="200">
          <template slot-scope="scope">
            <span v-if='scope.row.val.indexOf("http://")==-1'>{{scope.row.val}}</span>
            <img v-else :src='scope.row.val' width='50px' height='50px' alt='' />
          </template>
        </el-table-column>
        <el-table-column prop="introduce" label="介绍" width="200"></el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
                <el-button @click="toEditClick(scope.row)" type="text" size="small">编辑</el-button>
                <el-button type="text" size="small" @click='toDeleteClick(scope.row.id)'>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 表格结束 -->
    <!-- 模态框开始 -->
    <el-dialog  :title="title" :visible.sync="visible" class='dialog'>
    <el-form  :model="form"  ref='myForm'>
    <el-form-item label="名称" :label-width="formLabelWidth">
      <el-input v-model="form.name" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="介绍" :label-width="formLabelWidth">
      <el-input v-model="form.introduce" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item label="类型" :label-width="formLabelWidth">
      <el-select v-model="type">
        <el-option label="文字" value="文字"></el-option>
        <el-option label="图片" value="图片"></el-option>
      </el-select>
    </el-form-item>
    <el-form-item v-if='type==="文字"' label="值" :label-width="formLabelWidth">
      <el-input v-model="form.val" autocomplete="off"></el-input>
    </el-form-item>
    <el-form-item v-else label="图片" :label-width="formLabelWidth" >
          <el-upload
            class="avatar-uploader"
            :action="uploadFileURL"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="imageUrl" :src="form.val" class="avatar" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
  </el-form>
  <div slot="footer" class="dialog-footer">
    <el-button @click="visible = false">取 消</el-button>
    <el-button type="primary" @click="toSubmit('myForm')">确 定</el-button>
  </div>
</el-dialog>
    <!-- 模态框结束 -->
  </div>
</template>
<script>
import { showFileURL, uploadFileURL } from "@/utils/config";
import {baseConfig,deleteById,saveOrConfigUpdate} from '@/api/config'
import { get } from "@/utils/request";
export default {
  data() {
    return {
      showFileURL,
      uploadFileURL,
      tableData: [],
      imageUrl:'',
      visible:false,
      form:{},
      type:'文字',
      formLabelWidth:'160px',
      title:'',
    };
  },
  watch:{
      type(){
          this.form.val='';
          this.imageUrl=''
      }
  },
  created() {
    this.findAllConfig();
  },
  methods: {
    //   给新增按钮绑定事件
    toAdd(){
        this.visible=true;
        this.title = '新增基础配置信息';
        //添加模态框弹出来数据都是空的
        this.form = {};
        //  this.findAllConfig();
    },
      handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(res);
      // 展示图片 图片基础路径+图片地址
      this.form.val = showFileURL + res.data.id;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    //   封装查询所有配置信息方法
    async findAllConfig() {
      let res = await get("/baseConfig/findAll").then(res => {
        this.tableData = res.data;
      });
    },
    //给确定按钮绑定事件 执行保存-->新增 或者更新-->修改操作
        toSubmit(formName) {//表单进行校验
        this.$refs[formName].validate(async valid => {
        if (valid) {
            //校验通过 发送请求
          let res = await  saveOrConfigUpdate(this.form);
          this.visible = false;
        //   console.log(res);
          this.$message({
            message: res.message,
            type: "success"
          });
          // 自动刷新页面
          this.findAllConfig();
        } 
      });
    },
     //给编辑按钮绑定事件
      toEditClick(row) {
        this.visible = true;
        this.title = '修改配置信息';
        //把每行的数据name,icon,num一个一个解构出来显示到页面上
        this.form = {
          ...row
        };
          // this.findAllConfig();
      },
      //给删除按钮绑定事件
      toDeleteClick(id) {
        //删除进行弹框提示
        this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          //确定删除向后台发出请求进行删除
          //id是一个字符串
          let res = await deleteById({
            id: id
          }); //async await要放在最近的函数中
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.findAllConfig();
        }).catch(() => {
          //点击取消 提示用户删除
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
  }
};
</script>
<style lang="scss">
.dialog{
  height: 800px;
}
dialog-footer{
  height: 200px;
}
</style>


